import 'dart:math';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yi_shop/model/brand.dart';
import 'package:yi_shop/ui/scroll_refresh_config.dart';
import 'package:yi_shop/utils/colors.dart';
import 'package:yi_shop/utils/styles.dart';
import 'package:yi_shop/utils/svg.dart';
import 'package:yi_shop/utils/tools.dart';

/// 品牌制造
class BrandManufactureController extends GetxController {
  RxList<BrandManufactureModel> pageList = RxList([]);

  RefreshController refreshController = RefreshController();

  RxInt total = 45.obs;

  @override
  void onInit() {
    initData();
    super.onInit();
  }

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > total.value) {
      return refreshController.loadNoData();
    }

    /// 添加数据
    initData();

    /// 完毕
    refreshController.loadComplete();
  }

  void initData() {
    var r = Random.secure();
    List<BrandManufactureModel> brandList = [
      BrandManufactureModel(
          name: 'HUAWEI',
          price: r.nextInt(1000000).toDouble(),
          imgUrl:
              'https://img1.baidu.com/it/u=713192780,3288894073&fm=253&fmt=auto&app=138&f=JPEG?w=549&h=500',
          like: r.nextInt(100000),
          comment: r.nextInt(100),
          total: r.nextInt(200)),
      BrandManufactureModel(
          name: 'HUAWEI',
          price: r.nextInt(1000000).toDouble(),
          imgUrl:
              'https://img0.baidu.com/it/u=3162832527,2865553814&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
          like: r.nextInt(100000),
          comment: r.nextInt(100),
          total: r.nextInt(200)),
      BrandManufactureModel(
          name: 'HUAWEI',
          price: r.nextInt(1000000).toDouble(),
          imgUrl:
              'https://img0.baidu.com/it/u=1996343225,3808403639&fm=253&fmt=auto&app=138&f=JPG?w=400&h=400',
          like: r.nextInt(100000),
          comment: r.nextInt(100),
          total: r.nextInt(200)),
      BrandManufactureModel(
          name: 'HUAWEI',
          price: r.nextInt(1000000).toDouble(),
          imgUrl:
              'https://img0.baidu.com/it/u=41002518,307079760&fm=253&fmt=auto&app=138&f=JPEG?w=250&h=242',
          like: r.nextInt(100000),
          comment: r.nextInt(100),
          total: r.nextInt(200)),
      BrandManufactureModel(
          name: 'HUAWEI',
          price: r.nextInt(1000000).toDouble(),
          imgUrl:
              'https://img.alicdn.com/img/i1/46014076/O1CN01jHkYtT1fyrFOX6OOD_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp',
          like: r.nextInt(100000),
          comment: r.nextInt(100),
          total: r.nextInt(200)),
      BrandManufactureModel(
          name: 'HUAWEI',
          price: r.nextInt(1000000).toDouble(),
          imgUrl:
              'https://img.alicdn.com/img/i1/409470085/O1CN01DoF4Jw1CUyWG6SdnB_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp',
          like: r.nextInt(100000),
          comment: r.nextInt(100),
          total: r.nextInt(200)),
      BrandManufactureModel(
          name: 'HUAWEI',
          price: r.nextInt(1000000).toDouble(),
          imgUrl:
          'https://img.alicdn.com/img/i4/131262746/O1CN01LTiH6S1W9iVAzdZm8_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp',
          like: r.nextInt(100000),
          comment: r.nextInt(100),
          total: r.nextInt(200)),
    ];
    pageList.addAll(brandList);
  }
}

class BrandManufacturePage extends GetView<BrandManufactureController> {
  const BrandManufacturePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: ToolsUtils.builderLeading(),
        title: const Text('品牌制造商'),
      ),
      body: Obx(() => scrollRefreshConfig(context)),
    );
  }

  ScrollRefreshConfig scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: controller.refreshController,
      onRefresh: controller.onRefresh,
      onLoading: controller.onLoading,
      child: listViewBuilder(context),
    );
  }

  ListView listViewBuilder(BuildContext context) {
    return ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        final row = controller.pageList[index];
        return Center(
          child: Container(
            width: 340,
            margin: EdgeInsets.only(top: 15.h),
            decoration: const BoxDecoration(color: Colors.white),
            child: Column(
              children: [
                ClipRRect(
                  borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(5.r), topRight: Radius.circular(5.r)),
                  child: Stack(
                    children: [
                      CachedNetworkImage(
                        imageUrl: row.imgUrl,
                        height: 160,
                        width: 340,
                        fit: BoxFit.fill,
                        // color: ColorsUtils.rgba(251, 184, 196, 1),
                        // colorBlendMode: BlendMode.colorBurn,
                      ),
                      Positioned(
                          top: 20,
                          left: 0,
                          right: 0,
                          child: Center(
                            child: Text(
                              '${row.name} 制造商',
                              style: StylesUtils.customTextStyle(fontSize: StylesUtils.fontSize20),
                            ),
                          )),
                      Positioned(
                          top: 50,
                          left: 0,
                          right: 0,
                          child: Center(
                            child: Text(
                              '${row.price}元起',
                              style: StylesUtils.customTextStyle(color: ColorsUtils.colorF2f2),
                            ),
                          ))
                    ],
                  ),
                ),
                Container(
                  height: 50,
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(5.r), bottomRight: Radius.circular(5.r))),
                  padding: EdgeInsets.symmetric(horizontal: 15.w),
                  alignment: Alignment.centerLeft,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      RichText(
                          text: TextSpan(children: [
                        WidgetSpan(
                            child: SvgUtils.assetName('u4413', width: 18, height: 15),
                            alignment: PlaceholderAlignment.middle),
                        TextSpan(
                            text: ' ${row.like}+',
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color9999, fontSize: StylesUtils.fontSize13))
                      ])),
                      RichText(
                          text: TextSpan(children: [
                        WidgetSpan(
                            child: SvgUtils.assetName('u4415', width: 18, height: 15),
                            alignment: PlaceholderAlignment.middle),
                        TextSpan(
                            text: ' 好评率 ${row.comment}%',
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color9999, fontSize: StylesUtils.fontSize13))
                      ])),
                      RichText(
                          text: TextSpan(children: [
                        TextSpan(
                            text: '共 ',
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color9999, fontSize: StylesUtils.fontSize13)),
                        TextSpan(
                            text: '${row.total}',
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.colorF26f,
                                fontSize: StylesUtils.fontSize13,
                                fontWeight: FontWeight.w700)),
                        TextSpan(
                            text: ' 件商品 ',
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color9999, fontSize: StylesUtils.fontSize13)),
                        WidgetSpan(
                            child: Icon(
                              Icons.arrow_forward_ios,
                              size: 12,
                              color: ColorsUtils.parseColorFromHexString(ColorsUtils.color9999),
                            ),
                            alignment: PlaceholderAlignment.middle),
                      ])),
                    ],
                  ),
                )
              ],
            ),
          ),
        );
      },
      itemCount: controller.pageList.length,
    );
  }
}
